package registnet.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import org.apache.torque.TorqueException;
import org.apache.torque.util.Criteria;


public class EmployeePeer extends registnet.dao.BaseEmployeePeer {
	
	
	/**
	 * Default constructor
	 */
	public EmployeePeer(){}
	
	
	/**
	 * Inserts a new employee
	 * 
	 * @param employeeNumber - The unique number assigned to an employee 
	 * @param firstName - The first name the employee
	 * @param middleName - The middle name the employee
	 * @param lastName - The last name the employee
	 * @param rank - The rank of the employee
	 * @param unitId - The autogenerated id of the unit
	 * @return	The autogenerated id of the employee
	 */
	public Integer createNewEmployee(String employeeNumber, String firstName, String middleName, String lastName, String rank, Integer unitId){
		Integer id = null;
		Criteria criteria = new Criteria();
		criteria.add(EmployeePeer.EMPLOYEENUMBER, employeeNumber);
		criteria.and(EmployeePeer.FIRSTNAME, firstName);
		criteria.and(EmployeePeer.MIDDLENAME, middleName);
		criteria.and(EmployeePeer.LASTNAME, lastName);
		criteria.and(EmployeePeer.RANK, rank);
		criteria.and(EmployeePeer.UNITID, unitId);
		
		try{
			id = Integer.parseInt(EmployeePeer.doInsert(criteria).toString());
		}catch (TorqueException e) {
			e.printStackTrace();
		}
		return id;
	}
	
	
	/**
	 * Gets the autogenerated id of the employee
	 * 
	 * @param employeeNumber - The unique number assigned to an employee 
	 * @return	The autogenerated id of the employee
	 */
	public Integer getEmployeeId(String employeeNumber){
		Criteria criteria = new Criteria();
		criteria.add(EmployeePeer.EMPLOYEENUMBER, employeeNumber);
		List list;
		try{
			list = EmployeePeer.doSelect(criteria);
			Employee employee = (Employee)list.get(0);
			return employee.getEmployeeId();
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the employee with the given id
	 * 
	 * @param employeeId - The autogenerated id of the employee
	 * @return	The employee with the given id
	 */
	public Employee getEmployee(Integer employeeId){
		Criteria criteria = new Criteria();
		criteria.add(EmployeePeer.EMPLOYEEID, employeeId);
		List list;
		try{
			list = EmployeePeer.doSelect(criteria);
			return (Employee)list.get(0);
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the employee with the given employee number
	 * 
	 * @param employeeNumber - The autogenerated id of the employee
	 * @return	The employee with the given employee number
	 */
	public Employee getEmployee(String employeeNumber){
		Criteria criteria = new Criteria();
		criteria.add(EmployeePeer.EMPLOYEENUMBER, employeeNumber);
		try{
			return (Employee)EmployeePeer.doSelect(criteria).get(0);
		}catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Updates the employee with the given id
	 * 
	 * @param employeeId - The autogenerated id of the employee
	 * @param employeeNumber - The unique number assigned to an employee 
	 * @param firstName - The first name the employee
	 * @param middleName - The middle name the employee
	 * @param lastName - The last name the employee
	 * @param rank - The rank of the employee
	 * @param unitId - The autogenerated id of the unit
	 * @return	true if the update is successful, false otherwise
	 */
	public boolean updateEmployee(Integer employeeId, String employeeNumber, String firstName, String middleName, String lastName, String rank, Integer unitId){
		Criteria criteria = new Criteria();
		criteria.add(EmployeePeer.EMPLOYEEID, employeeId);
		criteria.and(EmployeePeer.EMPLOYEENUMBER, employeeNumber);
		criteria.and(EmployeePeer.FIRSTNAME, firstName);
		criteria.and(EmployeePeer.MIDDLENAME, middleName);
		criteria.and(EmployeePeer.LASTNAME, lastName);
		criteria.and(EmployeePeer.RANK, rank);
		criteria.and(EmployeePeer.UNITID, unitId);
		try{
			EmployeePeer.doUpdate(criteria);
			return true;
		}catch (TorqueException e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	/**
	 * Deletes the employee with the given id
	 * 
	 * @param employeeId - The autogenerated id of the employee
	 * @return	true if the delete is successful, false otherwise
	 */
	public boolean deleteEmployee(Integer employeeId){
		Criteria criteria = new Criteria();
		criteria.add(EmployeePeer.EMPLOYEEID, employeeId);
		try{
			EmployeePeer.doDelete(criteria);
			return true;
		}catch (TorqueException e){
			e.printStackTrace();
			return false;
		}
	}
	
	
	/**
	 * Gets all the employees
	 * 
	 * @return	All the employees
	 */
	public List getAllEmployees(){
		Criteria criteria = new Criteria();
		criteria.addAscendingOrderByColumn(EMPLOYEENUMBER);
		try{
			return EmployeePeer.doSelect(criteria);
		}
		catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the employees with the given rank
	 * 
	 * @param rank - The rank of the employee
	 * @return	The employees with the given rank
	 */
	public List getEmployeeByRank(String rank){
		Criteria criteria = new Criteria();
		criteria.add(EmployeePeer.RANK, rank);
		criteria.addAscendingOrderByColumn(EMPLOYEENUMBER);
		try{
			return EmployeePeer.doSelect(criteria);
		}
		catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the employees with the given unit
	 * 
	 * @param unitId - The autogenerated id of the unit
	 * @return	The employees with the given unit
	 */
	public List getEmployeeByUnit(Integer unitId){
		Criteria criteria = new Criteria();
		criteria.add(EmployeePeer.UNITID, unitId);
		criteria.addAscendingOrderByColumn(EMPLOYEENUMBER);
		try{
			return EmployeePeer.doSelect(criteria);
		}
		catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
	
	/**
	 * Gets the employees with the given college
	 * 
	 * @param collegeId - The autogenerated id of the college
	 * @return
	 */
	public List getEmployeeByCollege(Integer collegeId){
		Criteria criteria = new Criteria();
		criteria.addJoin(EmployeePeer.UNITID, UnitPeer.UNITID);
		criteria.add(UnitPeer.COLLEGEID, collegeId);
		criteria.addAscendingOrderByColumn(EMPLOYEENUMBER);
		try{
			return EmployeePeer.doSelect(criteria);
		}
		catch (TorqueException e){
			e.printStackTrace();
			return null;
		}
	}
	
}
